/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity.tta;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

/**
 *
 * @author Chen Zhe
 */
/**
 * TTA conventions of type in StrLongRecord and StrDblRecord:
 *  types in StrLongRecord:  MonthSale,MonthSurvey,AgeGroupSale,CountrySale
 *  types in StrLongRecord: MonthReservation, MonthAdClick,AgeGroupReservation,CountryReservation
 */
@Entity
public class DataModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP) 
    protected Calendar time_stamp;
    protected Long[][] cluster;
    //the cluster data that only keeps record within 2 years
    private Long[][] clusterW2Y;
    //the countries list should be updated from the customer account registration side
//    protected String countriesList = 
//            // Southeast Asia
//            "Thailand, Vietnam, Other Countries in Southeast Asia, "+ 
//            // North Asia
//            "P R China, Taiwan, Hong Kong SAR, Japan, South Koria, Other Countries in North Asia, "+
//            // South Asia
//            "Bangladesh, India, Nepal, Pakistan, Sri Lanka, Other Countries in South Asia, "+ 
//            // West Asia
//            "Iran, Israel, Kuwait, Saudi Arabia, United Arab Emirates, Other Countries in West Asia, "+
//            // Europe
//            "Austria, Belgium & Luxembourg, Denmark, Finland, France, Germany, Greece, Italy, Netherlands, "+
//            "Norway, Porland, Rep of Ireland, Russian Federation, Spain, Sweden, Switzerland, Turkey, UK, "+
//            "Other Countries in Eastern Europe, Other Countries in Western Europe, "+
//            // Oceania
//            "Australia, New Zealand, Other Countries in Oceania, "+ 
//            // Africa
//            "Egypt, Mauritius, South Africa (Rep of), Other Countries in Africa"+ 
//            // Americas
//            "Canada, USA, Other Countries in Americas";
//    protected String ageGroupsList = "0-12, 13-20, 20-30, 30-40, 40-50, 50-60, above 60";
    //types in StrDblRecords: MonthSale,AgeGroupSale,CountrySale
    @OneToMany(cascade = {CascadeType.PERSIST})
    private List<StrDblRecord> strDblRecords = new ArrayList<StrDblRecord>();
    //types in StrLongRecord: MonthReservation,AgeGroupReservation,CountryReservation
    @OneToMany(cascade = {CascadeType.PERSIST})
    private List<StrLongRecord> strLongRecords = new ArrayList<StrLongRecord>();

    public DataModel() {
        this.create();
    }

    public void create() {
        Properties p = new Properties();
        try {
            InputStream ip = this.getClass().getResourceAsStream(".//cluster.properties");
            p.load(ip);
            ip.close();
        } catch (Exception e) {
        }
        String[] countriesArray = p.getProperty("countriesList").split(",");
//        System.out.println("numCountries:" + countriesArray.length);
        String[] ageGroupsArray = p.getProperty("ageGroupsList").split(",");

        //add in SaleAgeGroup, SaleCountryGroup, ReservationAgeGroup,ReservationCountryGroup
        cluster = new Long[countriesArray.length][ageGroupsArray.length];
        clusterW2Y = new Long[countriesArray.length][ageGroupsArray.length];
        time_stamp = Calendar.getInstance();
        time_stamp.set(2007,1,1,0,0,0);
        //initialize the cluster data mining structure
        for (int i = 0; i < cluster.length; i++) {
            for (int j = 0; j < cluster[0].length; j++) {
                cluster[i][j] = new Long(0);
            }
        }
        for (int i = 0; i < clusterW2Y.length; i++) {
            for (int j = 0; j < clusterW2Y[0].length; j++) {
                clusterW2Y[i][j] = new Long(0);
            }
        }
        
//        for (int i = 0; i < countriesArray.length; i++) {
//            StrDblRecord sdr = new StrDblRecord();
//            sdr.setAttribute(countriesArray[i].trim());
//            sdr.setNumDouble(new Double(0));
//            sdr.setType("SaleCountry");
//            strDblRecords.add(sdr);
//            StrLongRecord slr = new StrLongRecord();
//            slr.setAttribute(countriesArray[i].trim());
//            slr.setNumberLong(new Long(0));
//            slr.setType("ReservationCountry");
//            strLongRecords.add(slr);
//        }
//
//        for (int i = 0; i < ageGroupsArray.length; i++) {
//            StrDblRecord sdr = new StrDblRecord();
//            sdr.setAttribute(ageGroupsArray[i]);
//            sdr.setNumDouble(new Double(0));
//            sdr.setType("SaleAgeGroup");
//            strDblRecords.add(sdr);
//            StrLongRecord slr = new StrLongRecord();
//            slr.setAttribute(ageGroupsArray[i]);
//            slr.setNumberLong(new Long(0));
//            slr.setType("ReservationAgeGroup");
//            strLongRecords.add(slr);
//        }

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Calendar getTime_stamp() {
        return time_stamp;
    }

    public void setTime_stamp(Calendar time_stamp) {
        this.time_stamp = time_stamp;
    }

    public void updateCluster() {
    }

    public Long[][] getCluster() {
        return cluster;
    }

    public void setCluster(Long[][] cluster) {
        this.cluster = cluster;
    }

    public void setStrDblRecords(List<StrDblRecord> strDblRecords) {
        this.strDblRecords = strDblRecords;
    }

    public List<StrLongRecord> getStrLongRecords() {
        return strLongRecords;
    }

    public void setStrLongRecords(List<StrLongRecord> strLongRecords) {
        this.strLongRecords = strLongRecords;
    }

    public List<StrDblRecord> getStrDblRecords() {
        return strDblRecords;
    }
    
    public void addStrDblRecord(StrDblRecord record){
        this.strDblRecords.add(record);
    }
    
    public void addStrLongRecord(StrLongRecord record){
        this.strLongRecords.add(record);
    }
    
    public String[] getCountriesArray(){
        Properties p = new Properties();
        try {
            InputStream ip = this.getClass().getResourceAsStream(".//cluster.properties");
            p.load(ip);
            ip.close();

        } catch (Exception e) {
        }
        String[] countriesArray = p.getProperty("countriesList").split(",");
        return countriesArray;
    }

    public String[] getAgeGroupsArray() {
        Properties p = new Properties();
        try {
            InputStream ip = this.getClass().getResourceAsStream(".//cluster.properties");
            p.load(ip);
            ip.close();
        } catch (Exception e) {
        }
        String[] ageGroupsArray = p.getProperty("ageGroupsList").split(",");
        return ageGroupsArray;
    }

    /**
     * @return the clusterW2Y
     */
    public Long[][] getClusterW2Y() {
        return clusterW2Y;
    }

    /**
     * @param clusterW2Y the clusterW2Y to set
     */
    public void setClusterW2Y(Long[][] clusterW2Y) {
        this.clusterW2Y = clusterW2Y;
    }
    /**
     * List of countries:
     * 1. Southeast Asia: Thailand, Vietnam, Other Countries in Southeast Asia
     * 2. North Asia: P R China, Taiwan, Hong Kong SAR, Japan, South Koria, Other Countries in North Asia
     * 3. South Asia: Bangladesh, India, Nepal, Pakistan, Sri Lanka, Other Countries in South Asia
     * 4. West Asia: Iran, Israel, Kuwait, Saudi Arabia, United Arab Emirates, Other Countries in West Asia
     * 5. Europe: Austria, Belgium & Luxembourg, Denmark, Finland, France, Germany, Greece, Italy, Netherlands,
    Norway, Porland, Rep of Ireland, Russian Federation, Spain, Sweden, Switzerland, Turkey, UK, 
    Other Countries in Eastern Europe, Other Countries in Western Europe
     * 6. Occeania: Australia, New Zealand, Other Countries in Oceania
     * 7. Africa: Egypt, Mauritius, South Africa (Rep of), Other Countries in Africa
     * 8. Americas: Canada, USA, other countries in Americas
     */
    /**
     * Age: 0-12, 13-20, 20-30, 30-40, 40-50, 50-60, above 60
     */
}
